package com.demo.maintenance.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.demo.maintenance.R;
import com.demo.maintenance.bean.CourseBean;
import com.demo.maintenance.contant.Global;
import com.demo.maintenance.utils.AntiShakeUtils;
import com.demo.maintenance.utils.HttpManager;
import com.demo.maintenance.utils.SystemBarTintManager;
import com.demo.maintenance.utils.TimeUtils;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;
import java.util.List;

public class CourseListActivity extends AppCompatActivity {

    private RecyclerView recycler_view;
    private ListAdapter listAdapter;
    private List<CourseBean.Data> datasource = new ArrayList<>();
    private FloatingActionButton btn_create;
    private long childId = -1;
    private String childSex = null;
    private String childName = null;
    private String childClass = null;

    private ImageView iv_sex;
    private TextView tv_student_name;
    private TextView tv_student_class;

    @Override
    protected void onResume() {
        super.onResume();
        requestData();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        QMUIStatusBarHelper.translucent(this);
        SystemBarTintManager.setStatusBarDarkTheme(this, true);
        setContentView(R.layout.activity_list_course);

        findViewById(R.id.common_back_iv).setOnClickListener(v -> finish());
        ((TextView) findViewById(R.id.common_content_tv)).setText("Course List");

        if (getIntent() != null){
            childId = getIntent().getLongExtra("childId", -1L);
            childSex = getIntent().getStringExtra("childSex");
            childName = getIntent().getStringExtra("childName");
            childClass = getIntent().getStringExtra("childClass");
        }

        iv_sex = findViewById(R.id.iv_sex);
        if ("0".equals(childSex)){
            iv_sex.setImageResource(R.mipmap.boy);
        }else {
            iv_sex.setImageResource(R.mipmap.girl);
        }
        tv_student_name = findViewById(R.id.tv_student_name);
        tv_student_name.setText(childName);
        tv_student_class = findViewById(R.id.tv_student_class);
        tv_student_class.setText(childClass);

        listAdapter = new ListAdapter();
        recycler_view = findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
        recycler_view.setAdapter(listAdapter);


    }

    private void requestData() {

        WaitDialog.show("Waiting");

        String url = Global.ipAddress + "/system/course/listAll";
        if (childId != -1){
            url += "?childId=" + childId;
        }

        try {
            HttpManager.get(url, new HttpManager.NetworkCallBack() {
                @Override
                public void onSuccess(String successInfo) {
                    try {
                        Log.e("monitoring", successInfo);

                        CourseBean responseBean = JSON.parseObject(successInfo, CourseBean.class);
                        if (responseBean.code == 200) {
                            runOnUiThread(() -> {
                                if (responseBean.rows != null && !responseBean.rows.isEmpty()) {
                                    datasource.clear();
                                    datasource.addAll(responseBean.rows);
                                    listAdapter.notifyDataSetChanged();
                                }
                            });
                        } else {

                            Log.e("monitoring", "get list failed !");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(String errorInfo) {
                    Log.e("monitoring", errorInfo);
                }

                @Override
                public void onFinal() {
                    super.onFinal();
                    WaitDialog.dismiss();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            WaitDialog.dismiss();
        }
    }

    private class ListAdapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(CourseListActivity.this)
                    .inflate(R.layout.recycler_item_course, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            CourseBean.Data item = datasource.get(position);
            holder.tv_course_name.setText(item.courseName);
            holder.tv_course_time.setText("1 Lesson (" + TimeUtils.format(item.courseDate) + ")");
            holder.tv_clazz.setText("Little Star");
            holder.btn_exam.setOnClickListener(v -> {
                if (AntiShakeUtils.isInvalidClick(v)){
                    return;
                }
                Intent intent = new Intent(CourseListActivity.this, QuizListActivity.class);
                intent.putExtra("courseId", item.courseId);
                intent.putExtra("childId", item.childId);
                startActivity(intent);
            });
            holder.btn_assignment.setOnClickListener(v-> {
                if (AntiShakeUtils.isInvalidClick(v)){
                    return;
                }
                Intent intent = new Intent(CourseListActivity.this, AssignListActivity.class);
                intent.putExtra("courseId", item.courseId);
                intent.putExtra("childId", item.childId);
                startActivity(intent);
            });
            setRandomColor(item.courseName, holder.v_left_bg);
        }

        @Override
        public int getItemCount() {
            return datasource.size();
        }
    }

    private void setRandomColor(String courseName, View bgView){
        if (TextUtils.isEmpty(courseName)){
            bgView.setBackgroundResource(android.R.color.holo_blue_light);
            return;
        }
        switch (courseName){
            case "English":
                bgView.setBackgroundResource(android.R.color.holo_green_dark);
                break;
            case "Art":
                bgView.setBackgroundResource(android.R.color.holo_red_dark);
                break;
            case "Math":
                bgView.setBackgroundResource(android.R.color.holo_purple);
                break;
            case "History":
                bgView.setBackgroundResource(android.R.color.holo_orange_dark);
                break;
            default:
                bgView.setBackgroundResource(android.R.color.holo_blue_light);
                break;
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_course_name;
        private TextView tv_course_time;
        private TextView tv_clazz;
        private Button btn_exam;
        private Button btn_assignment;
        private View v_left_bg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tv_course_name = itemView.findViewById(R.id.tv_course_name);
            tv_course_time = itemView.findViewById(R.id.tv_course_time);
            tv_clazz = itemView.findViewById(R.id.tv_clazz);
            btn_exam = itemView.findViewById(R.id.btn_exam);
            btn_assignment = itemView.findViewById(R.id.btn_assignment);
            v_left_bg = itemView.findViewById(R.id.v_left_bg);
        }
    }
}
